package com.example.schoolexam2.controller;

import com.example.schoolexam2.dto.StudentChooseDto;
import com.example.schoolexam2.entity.Exam;
import com.example.schoolexam2.vo.*;
import com.example.schoolexam2.enums.ResultEnum;
import com.example.schoolexam2.service.ExamService;
import com.example.schoolexam2.service.QuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam")
public class ExamController {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    QuestionService questionService;
    @Autowired
    ExamService examService;

    //创建考试
    @PostMapping("/createExam")
    ResultVO createExam(@RequestBody ExamCreateVo examCreateVo) {
        System.out.println(examCreateVo.toString());
        try {
            examService.createExam(examCreateVo);
            return new ResultVO(ResultEnum.CREATE_SUCCESS.getCode(), ResultEnum.REGISTER_SUCCESS.getMessage(), null);
        } catch (NullPointerException e) {
           return new ResultVO(ResultEnum.CREATE_FAILED.getCode(), ResultEnum.CREATE_FAILED.getMessage(), null);
        }
    }

    //获取当前班级的试卷
    @GetMapping("/getOwnAllExam")
    List<Exam> getOwnAllExam(String classInfo) {
        //通过学生班级的信息，查询当前班级需要考试的信息
        return examService.getOwnAllExam(classInfo);
    }

    //根据考试id获取考试所有信息，包括试题
    @GetMapping("/getExamAllInfo")
    ExamVo getExanAllInfo(String examId, String studentId) {
        try {
            if (studentId!=null){// 学生id不为空即为学生查询历史试卷详细
                return examService.getStudentChoose(examId,studentId);
            }else{
                return examService.getExam(examId);
            }
        } catch (Exception e) {
            return null;
        }
    }

    //保存当前学生对当前试卷的作答
    @PostMapping("/saveStudentOption")
    void saveStudentOption(@RequestBody StudentChooseDto studentChooseDto) {
        System.out.println(studentChooseDto);
        examService.saveStudentChoose(studentChooseDto);
    }

    //获取题目的完整信息
    @GetMapping("/getQuestionInfo")
    TestVO getQuestions() {
        return examService.test();
    }

    //通过学生id查询学生考过的考试
    @GetMapping("/getHistoryExam")
    List<ExamVo> getHistoryExam(String studentId) {
        logger.info("id为{}的学生访问了查询历史考试的接口", studentId);
        return examService.getHistoryExam(studentId);
    }

    //教师查询试卷
    @GetMapping("/getExam")
    PageVo findExam(String departmentId, String isPublic, String teacherId, String examName,
                    Integer currentpage,Integer size){
        return examService.teacherGetExam(departmentId,isPublic,teacherId,examName,currentpage,size);
    }
}
